package com.wwwarehouse.contract.query_express;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.common.collect.Lists;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.ExpressDetailBean;

import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

import static contract.wwwarehouse.com.contract.R.id.expDetailPro1Img;
import static contract.wwwarehouse.com.contract.R.id.item1Desc;


/**
 * Created by clannad.shao on 2017/11/4.
 *
 * @author clannad_sk
 * @date 2017/11/4
 * 订单跟踪fragment
 */
public class ExpressDetailItemFragment extends BaseFragment {

    private static final int PAGE_SIZE = 4;
    private boolean isItem1 = false;
    //已发货
    private static final int DETAIL_DELIVERED = 1055;
    //运输中（快递揽收+运输中）
    private static final int DETAIL_TAKING_EXPRESS = 1060;
    private static final int DETAIL_TRANSPORT = 1065;
    //派件中（派件中+派件失败+拒签）
    private static final int DETAIL_SENDING = 1080;
    private static final int DETAIL_SEND_FAILED = 1085;
    private static final int DETAIL_REJECTED = 1095;
    //已签收
    private static final int DETAIL_SIGN_IN = 1090;

    private static final int START_PAGE = 1;

    private static final int FIRST_ITEM = 1;
    private static final int SEC_ITEM = 2;
    private static final int THIRD_ITEM = 3;
    private static final int FOURTH_ITEM = 4;

    private TextView mDetailTitle;
    private TextView mDetailDelivered;
    private CircleImageView mExpDetailPro1Img;
    private TextView mDetailTransport;
    private CircleImageView mExpDetailPro2Img;
    private TextView mDetailSending;
    private CircleImageView mExpDetailPro3Img;
    private TextView mDetailSendTo;
    private CircleImageView mExpDetailPro4Img;
    private RelativeLayout mItem1Rl;
    private TextView mItem1Desc;
    private TextView mItem1Time;
    private RelativeLayout mItem2Rl;
    private TextView mItem2Desc;
    private TextView mItem2Time;
    private RelativeLayout mItem3Rl;
    private TextView mItem3Desc;
    private TextView mItem3Time;
    private RelativeLayout mItem4Rl;
    private TextView mItem4Desc;
    private TextView mItem4Time;
    private TextView mExpDetailSendAddress;
    private TextView mExpDetailRecAddress;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.express_detail_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //初始化view
        mDetailTitle = $(R.id.detailTitle);
        mDetailDelivered = $(R.id.detailDelivered);
        mExpDetailPro1Img = $(expDetailPro1Img);
        mDetailTransport = $(R.id.detailTransport);
        mExpDetailPro2Img = $(R.id.expDetailPro2Img);
        mDetailSending = $(R.id.detailSending);
        mExpDetailPro3Img = $(R.id.expDetailPro3Img);
        mDetailSendTo = $(R.id.detailSendTo);
        mExpDetailPro4Img = $(R.id.expDetailPro4Img);
        mItem1Rl = $(R.id.item1Rl);
        mItem1Desc = $(item1Desc);
        mItem1Time = $(R.id.item1Time);
        mItem2Rl = $(R.id.item2Rl);
        mItem2Desc = $(R.id.item2Desc);
        mItem2Time = $(R.id.item2Time);
        mItem3Rl = $(R.id.item3Rl);
        mItem3Desc = $(R.id.item3Desc);
        mItem3Time = $(R.id.item3Time);
        mItem4Rl = $(R.id.item4Rl);
        mItem4Desc = $(R.id.item4Desc);
        mItem4Time = $(R.id.item4Time);
        mExpDetailSendAddress = $(R.id.expDetailSendAddress);
        mExpDetailRecAddress = $(R.id.expDetailRecAddress);

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ExpressDetailItemFragment) {
            mActivity.setTitle(getString(R.string.query_express_detail));
        }
    }

    @Override
    public void requestDatas() {

    }


    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        initData();
        return null;
    }

    public void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            //bundle携带数据
            //快递名称
            String expressName = bundle.getString("expressName");
            //运单号
            String expressNo = bundle.getString("expressNo");
            //运单状态码
            int trackStatusCode = bundle.getInt("trackStatusCode");
            //收货地址
            String receiveAddress = bundle.getString("receiveAddress");
            //发货地址
            String sendAddress = bundle.getString("sendAddress");
            //数据源
            List<ExpressDetailBean.TrackListBean> trackList = bundle.getParcelableArrayList("trackList");
            mDetailTitle.setText((StringUtils.isNullString(expressName) ? "" : expressName) + "："
                    + (StringUtils.isNullString(expressNo) ? "" : expressNo));
            mExpDetailSendAddress.setText(StringUtils.isNullString(sendAddress) ? "" : sendAddress);
            mExpDetailRecAddress.setText(StringUtils.isNullString(receiveAddress) ? "" : receiveAddress);
            //TODO 设置按钮颜色
            if (trackStatusCode == DETAIL_DELIVERED) {
                //已发货
                mDetailDelivered.setBackgroundResource(R.drawable.express_detail_proback_shape);
                mDetailDelivered.setTextColor(getResources().getColor(R.color.white));
                mExpDetailPro1Img.setImageResource(R.color.common_color_c1_337cff);
                mExpDetailPro2Img.setImageResource(R.color.common_color_c8_d5d7dc);
                mExpDetailPro3Img.setImageResource(R.color.common_color_c8_d5d7dc);
                mExpDetailPro4Img.setImageResource(R.color.common_color_c8_d5d7dc);
            } else if (trackStatusCode == DETAIL_TAKING_EXPRESS || trackStatusCode == DETAIL_TRANSPORT) {
                //运输中（快递揽收+运输中）
                mDetailTransport.setBackgroundResource(R.drawable.express_detail_proback_shape);
                mDetailTransport.setTextColor(getResources().getColor(R.color.white));
                mExpDetailPro1Img.setImageResource(R.color.common_color_c8_d5d7dc);
                mExpDetailPro2Img.setImageResource(R.color.common_color_c1_337cff);
                mExpDetailPro3Img.setImageResource(R.color.common_color_c8_d5d7dc);
                mExpDetailPro4Img.setImageResource(R.color.common_color_c8_d5d7dc);

            } else if (trackStatusCode == DETAIL_SENDING || trackStatusCode == DETAIL_SEND_FAILED
                    || trackStatusCode == DETAIL_REJECTED) {
                //派件中（派件中+派件失败+拒签）
                mDetailSending.setBackgroundResource(R.drawable.express_detail_proback_shape);
                mDetailSending.setTextColor(getResources().getColor(R.color.white));
                mExpDetailPro1Img.setImageResource(R.color.common_color_c8_d5d7dc);
                mExpDetailPro2Img.setImageResource(R.color.common_color_c8_d5d7dc);
                mExpDetailPro3Img.setImageResource(R.color.common_color_c1_337cff);
                mExpDetailPro4Img.setImageResource(R.color.common_color_c8_d5d7dc);

            } else if (trackStatusCode == DETAIL_SIGN_IN) {
                //已签收
                mDetailSendTo.setBackgroundResource(R.drawable.express_detail_proback_shape);
                mDetailSendTo.setTextColor(getResources().getColor(R.color.white));
                mExpDetailPro1Img.setImageResource(R.color.common_color_c8_d5d7dc);
                mExpDetailPro2Img.setImageResource(R.color.common_color_c8_d5d7dc);
                mExpDetailPro3Img.setImageResource(R.color.common_color_c8_d5d7dc);
                mExpDetailPro4Img.setImageResource(R.color.common_color_c1_337cff);
            }
            int page = bundle.getInt("page");
            List<List<ExpressDetailBean.TrackListBean>> list = Lists.partition(trackList, 4);
            if (page > list.size()) {
                mItem1Rl.setVisibility(View.INVISIBLE);
                mItem2Rl.setVisibility(View.INVISIBLE);
                mItem3Rl.setVisibility(View.INVISIBLE);
                mItem4Rl.setVisibility(View.INVISIBLE);
                return;
            }

            if (page == START_PAGE && !list.get(page - 1).isEmpty()) {
                //第一页第一行
                mItem1Desc.setTextColor(getResources().getColor(R.color.common_color_c1_337cff));
            }
            setValue2View(list.get(page - 1));


        }
    }

    private void setValue2View(List<ExpressDetailBean.TrackListBean> result) {
        if (result.size() == FIRST_ITEM) {
            //只有一个item
            mItem1Desc.setText(result.get(0).getTrackDesc());
            mItem1Time.setText(result.get(0).getTrackTime());
            mItem1Rl.setVisibility(View.VISIBLE);
            mItem2Rl.setVisibility(View.INVISIBLE);
            mItem3Rl.setVisibility(View.INVISIBLE);
            mItem4Rl.setVisibility(View.INVISIBLE);
        } else if (result.size() == SEC_ITEM) {
            //有两个item
            mItem1Desc.setText(result.get(0).getTrackDesc());
            mItem1Time.setText(result.get(0).getTrackTime());
            mItem2Desc.setText(result.get(1).getTrackDesc());
            mItem2Time.setText(result.get(1).getTrackTime());
            mItem1Rl.setVisibility(View.VISIBLE);
            mItem2Rl.setVisibility(View.VISIBLE);
            mItem3Rl.setVisibility(View.INVISIBLE);
            mItem4Rl.setVisibility(View.INVISIBLE);
        } else if (result.size() == THIRD_ITEM) {
            //有三个item
            mItem1Desc.setText(result.get(0).getTrackDesc());
            mItem1Time.setText(result.get(0).getTrackTime());
            mItem2Desc.setText(result.get(1).getTrackDesc());
            mItem2Time.setText(result.get(1).getTrackTime());
            mItem3Desc.setText(result.get(2).getTrackDesc());
            mItem3Time.setText(result.get(2).getTrackTime());
            mItem1Rl.setVisibility(View.VISIBLE);
            mItem2Rl.setVisibility(View.VISIBLE);
            mItem3Rl.setVisibility(View.VISIBLE);
            mItem4Rl.setVisibility(View.INVISIBLE);
        } else {
            //有四个item
            mItem1Desc.setText(result.get(0).getTrackDesc());
            mItem1Time.setText(result.get(0).getTrackTime());
            mItem2Desc.setText(result.get(1).getTrackDesc());
            mItem2Time.setText(result.get(1).getTrackTime());
            mItem3Desc.setText(result.get(2).getTrackDesc());
            mItem3Time.setText(result.get(2).getTrackTime());
            mItem4Desc.setText(result.get(3).getTrackDesc());
            mItem4Time.setText(result.get(3).getTrackTime());
            mItem1Rl.setVisibility(View.VISIBLE);
            mItem2Rl.setVisibility(View.VISIBLE);
            mItem3Rl.setVisibility(View.VISIBLE);
            mItem4Rl.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
